#   Copyright (c) 2008 ToI-Planning <office@toi-planning.net>
# 
#   Redistribution and use in source and binary forms, with or without
#   modification, are permitted provided that the following conditions
#   are met:
# 
#   1. Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#
#   2. Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions and the following disclaimer in the
#      documentation and/or other materials provided with the distribution.
#
#   3. Neither the name of the authors nor the names of its contributors
#      may be used to endorse or promote products derived from this
#      software without specific prior written permission.
#
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
#   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
#   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
#   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
#   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#  $Id: App.pm 13 2009-03-17 01:50:49Z usualoma $

package StyledFields::App;
use strict;
use File::Basename;

sub plugin_data_post_save {
	my ($cb, $obj, $original) = @_;
	my $plugin = MT->component('StyledFields');
	my $app = MT->instance;

	if (lc($app->param('plugin_sig')) ne $plugin->id) {
		return;
	}

	require MT::Blog;
	my $blog_id = $app->param('blog_id') || return;

    my %css_hash = ();
    my %css_hash2 = ();
	my @ids = $app->param('styled_fields_css_ids');
    foreach my $id (@ids) {
        $css_hash{$id} = $app->param('styled_fields_css_' . $id);
        $css_hash2{$id} = {
			'element1' => $app->param('styled_fields_css_element1_' . $id),
			'element2' => $app->param('styled_fields_css_element2_' . $id),
		};
    }

	$plugin->set_config_value('css_hash', \%css_hash, 'blog:' . $blog_id);
	$plugin->set_config_value('css_hash2', \%css_hash2, 'blog:' . $blog_id);
}

sub source_edit_entry {
	my ($cb, $app, $tmpl) = @_;
	my $plugin = MT->component('StyledFields');
	my $blog_id = $app->param('blog_id') or return;
	my @ids = ();

	my $css_hash = $plugin->get_config_value(
        'css_hash', 'blog:' . $blog_id
    ) || {};
	my $css_hash2 = $plugin->get_config_value(
        'css_hash2', 'blog:' . $blog_id
    ) || {};

    require CustomFields::Field;
	my @fields = CustomFields::Field->load({
		'blog_id' => [ 0, $blog_id ],
		'obj_type' => $app->param('_type'),
		'id' => [ keys(%$css_hash) ],
	});

    my $css = '';
	foreach my $f (@fields) {
		my $id = $f->id;
        if ($css_hash->{$id}) {
            $css .=
                '#customfield_' . $f->basename . '-field {' .
                $css_hash->{$id} .
                "}\n";
        }

		if ($css_hash2->{$id}) {
			my $type = $f->type;
			if ($type eq 'datetime') {
				$css .=
					'#d_customfield_' . $f->basename . ' {' .
					$css_hash2->{$id}{element1} .
					"}\n" .
					'#t_customfield_' . $f->basename . ' {' .
					$css_hash2->{$id}{element2} .
					"}\n";
			}
			elsif ($type eq 'select') {
				$css .=
					'#customfield_' . $f->basename . ' {' .
					$css_hash2->{$id}{element1} .
					"}\n" .
					'#customfield_' . $f->basename . ' option {' .
					$css_hash2->{$id}{element2} .
					"}\n";
			}
			elsif ($type eq 'radio') {
				$css .=
					'#customfield_' . $f->basename . '-field ul {' .
					$css_hash2->{$id}{element1} .
					"}\n" .
					'#customfield_' . $f->basename . '-field ul li {' .
					$css_hash2->{$id}{element2} .
					"}\n";
			}
			else {
				$css .=
					'#customfield_' . $f->basename . ' {' .
					$css_hash2->{$id}{element1} .
					"}\n";
			}
		}
	}

	if ($css) {
		my $style = <<__EOH__;
<style type="text/css">
$css
</style>
__EOH__

		my $replace = '<mt:?include[^>]*name="include/footer.tmpl"[^>]*>';
		$$tmpl =~ s#$replace#$style$&#i;
	}
}

1;
